/**
 * Created with JetBrains PhpStorm.
 * User: ishowshao
 * Date: 12-5-15
 * Time: 下午6:03
 */
var href = window.location.href;
var charIncrement = function (c) {
    var charCode = c.charCodeAt(0);
    if (charCode == 122) {
        charCode = 97;
    } else {
        charCode++;
    }
    return String.fromCharCode(charCode);
};
var increment = function (str) {
    var array = str.split('');

    if (array.length == 1) {
        array[0] = charIncrement(array[0]);
        if (array[0] == 'a') {
            array.push('a');
        }
    } else {
        var c, carry = false;
        array[0] = c = charIncrement(array[0]);
        if (c == 'a') {
            carry = true;
        }
        for (var i = 1; i < array.length; i++) {
            c = array[i];
            if (carry) {
                array[i] = c = charIncrement(c);
                if (c !== 'a') {
                    carry = false;
                }
            }
        }
        if (i == array.length && carry) {
            array.push('a');
        }
    }
    return array.join('');
};
if (href.indexOf('http://www.verydz.com/domain.php') >= 0) {
    if (href.indexOf('#') > 0) {
        var current = href.substr(href.indexOf('#') + 1);
        var inc = increment(current);
        window.location.href = href.substr(0, href.indexOf('#')) + '#' + inc;
        var form = document.getElementById('frm_Whois');
        form.action = form.action + '#' + inc;
        document.getElementsByName('domainname')[0].value = current;

        var tr;
        var domain = '';
        var has = false;
        if (tr = document.querySelectorAll('form.clear table tr:first-child')[0]) {
            if (tr.innerHTML.indexOf('.com') > 0 && tr.innerHTML.indexOf('未注册') > 0) {
                has = true;
                domain = tr.querySelectorAll('td')[0].innerText;
                var script = document.createElement('script');
                script.src = 'http://localhost:10088/domain/domain.php?domain=' + domain;
                document.getElementsByTagName('head')[0].appendChild(script);
            }
        }
        if (has) {
            setTimeout(function () {
                var e = document.createEvent('HTMLEvents');
                e.initEvent('submit', false, true);
                form.dispatchEvent(e);
                form.submit();
            }, 500);
        } else {
            var e = document.createEvent('HTMLEvents');
            e.initEvent('submit', false, true);
            form.dispatchEvent(e);
            form.submit();
        }
    } else {
        window.location.href = href + '#aajs';
    }
}
